// Copyright (C) 2012 Yury Bychenkov (http://bychenkov.com).
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//  - Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//  - Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//  - Neither the name of Yury Bychenkov nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef logger_h
#define logger_h

#include "common.h"

class Logger {
public:
	Logger(const char* name);

	template <typename T> void info(const T& p);
	template <typename T1, typename T2> void info(const T1& p1, const T2& p2);
	template <typename T1, typename T2, typename T3> void info(const T1& p1, const T2& p2, const T3& p3);

	template <typename T> void error(const T& p);
	template <typename T1, typename T2> void error(const T1& p1, const T2& p2);
	template <typename T1, typename T2, typename T3> void error(const T1& p1, const T2& p2, const T3& p3);

	template <typename T> void warn(const T& p);
	template <typename T1, typename T2> void warn(const T1& p1, const T2& p2);
	template <typename T1, typename T2, typename T3> void warn(const T1& p1, const T2& p2, const T3& p3);

private:
	ostream& out(const char* type);

private:
	const string name;
	ostream& os;
};


inline Logger::Logger(const char* name_): name(name_), os(cout) {
}

inline ostream& Logger::out(const char* type) {
	timeval tv;
	gettimeofday(&tv, NULL);

	time_t t = tv.tv_sec;
	struct tm ti = *localtime(&t); // TODO make thread-safe

	char buffer[128] = "\r";
	strftime(buffer + strlen(buffer), sizeof(buffer), "%Y-%m-%d %H:%M:%S", &ti);
	sprintf(buffer + strlen(buffer), ".%03d %s [%s] ", (uint32)(tv.tv_usec / 1000), type, name.c_str());

	return os << buffer << dec << boolalpha;
}

template <typename T>
void Logger::info(const T& p) {
	out("I") << p << endl;
}

template <typename T1, typename T2>
void Logger::info(const T1& p1, const T2& p2) {
	out("I") << p1 << p2 << endl;
}

template <typename T1, typename T2, typename T3>
void Logger::info(const T1& p1, const T2& p2, const T3& p3) {
	out("I") << p1 << p2 << p3 << endl;
}

template <typename T>
void Logger::error(const T& p) {
	out("E") << p << endl;
}

template <typename T1, typename T2>
void Logger::error(const T1& p1, const T2& p2) {
	out("E") << p1 << p2 << endl;
}

template <typename T1, typename T2, typename T3>
void Logger::error(const T1& p1, const T2& p2, const T3& p3) {
	out("E") << p1 << p2 << p3 << endl;
}

template <typename T>
void Logger::warn(const T& p) {
	out("W") << p << endl;
}

template <typename T1, typename T2>
void Logger::warn(const T1& p1, const T2& p2) {
	out("W") << p1 << p2 << endl;
}

template <typename T1, typename T2, typename T3>
void Logger::warn(const T1& p1, const T2& p2, const T3& p3) {
	out("W") << p1 << p2 << p3 << endl;
}

#endif // logger_h
